package com.fengzuxiao.weatherdemo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.fengzuxiao.weatherdemo.city_manager.CityMangerActivity;
import com.fengzuxiao.weatherdemo.db.DBManger;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView addCity, more;
    private LinearLayout pointLayout;
    private ViewPager mainVp;

    //ViewPager数据源
    List<Fragment> fragmentList;
    List<String> cityList;
    List<ImageView> imageList;
    private CityFragmentPagerAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        addCity = findViewById(R.id.iv_add);
        more = findViewById(R.id.iv_more);
        pointLayout = findViewById(R.id.layout_top);
        mainVp = findViewById(R.id.main_vp);
        addCity.setOnClickListener(this);
        more.setOnClickListener(this);

        fragmentList = new ArrayList<>();
        cityList = DBManger.queryAllCityName(); //获取数据库包含的城市信息列表
        imageList = new ArrayList<>();

        if (cityList.size() == 0) {
            cityList.add("西安");
        }
        /**
         * 搜索界面点击跳转此界面，会传值，此处获取
         */
        Intent intent = getIntent();
        String city = intent.getStringExtra("city");
        if (!cityList.contains(city)&&!TextUtils.isEmpty(city)){
            cityList.add(city);
        }
        //初始化ViewPager页面的方法
        initPager();
        adapter = new CityFragmentPagerAdapter(getSupportFragmentManager(), fragmentList);
        mainVp.setAdapter(adapter);
        //创建小圆点指示器
        initPrint();
        //设置最后一个城市信息
        mainVp.setCurrentItem(fragmentList.size() - 1);
        //设置viewpager页面监听
        setPagerListener();

    }

    private void setPagerListener() {
        /*设置监听事件*/
        mainVp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                for (int i = 0; i < imageList.size(); i++) {
                    imageList.get(i).setImageResource(R.mipmap.a1);
                }
                imageList.get(position).setImageResource(R.mipmap.a2);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initPrint() {
        //创建小圆点
        for (int i = 0; i < fragmentList.size(); i++) {
            ImageView pIv = new ImageView(this);
            pIv.setImageResource(R.mipmap.a1);
            pIv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
            LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) pIv.getLayoutParams();
            lp.setMargins(0, 0, 20, 0);
            imageList.add(pIv);
            pointLayout.addView(pIv);
        }
        imageList.get(imageList.size() - 1).setImageResource(R.mipmap.a2);

    }

    private void initPager() {
        for (int i = 0; i < cityList.size(); i++) {
            WeatherFragment weatherFragment = new WeatherFragment();
            Bundle bundle = new Bundle();
            bundle.putString("city", cityList.get(i));
            weatherFragment.setArguments(bundle);
            fragmentList.add(weatherFragment);
        }
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.iv_add:
                intent.setClass(this, CityMangerActivity.class);
                break;
            case R.id.iv_more:
                intent.setClass(this, MoreActivity.class);
                break;
        }
        startActivity(intent);
    }

    /**
     * 当页面重新加载时会调用的函数，这个函数在页面获取焦点之前进行调用，
     * 此处完成viewpager页数的更新
     */
    @Override
    protected void onRestart() {
        super.onRestart();
        //获取数据库当中还剩下的城市集合
        List<String> list = DBManger.queryAllCityName();
        if (list.size()==0) {
            list.add("西安");
        }
        cityList.clear(); //重写加载之前，清空原本数据
        cityList.addAll(list);
        //剩余城市也要创建对应的fragment页面
        fragmentList.clear();
        initPager();
        adapter.notifyDataSetChanged();
        //页面数量发生改变时，指示器的数量也会发生变化，重新设置添加指示器
        imageList.clear();
        pointLayout.removeAllViews();
        initPrint();
        mainVp.setCurrentItem(fragmentList.size()-1);
    }
}
